﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Arbalest.Web.Services;
using Arbalest.Models;
using Arbalest.Web.Helpers;

namespace Arbalest.Web.Controllers
{
    public class CompanyController : Controller
    {
        #region comment out (repository pattern)
        
        //private ICompanyService services; //  = new CompanyServiceProvider(this.model)

        //public CompanyController ()
        //{
        //    this.services = new CompanyService(
        //        new ModelStateService(this.ModelState), 
        //        new CompanyRepository()
        //    );
        //    //this.services = new Scofield.Web.Services.Providers.SchoolUserServicesProvider(
        //    //    new Scofield.Web.Services.Providers.ModelStateServicesProvider(this.ModelState),
        //    //    new Scofield.Web.Models.Repositories.SchoolUserRepository());
        //}

        //public CompanyController(ICompanyService services)
        //{
        //    this.services = services;
        //}
        #endregion comment out (repository pattern)

        //
        // GET: /Company/

        public ActionResult Index()
        {
            DataStore ds = new DataStore();
            return View(ds.GetList<Company>());
        }

        //
        // GET: /Company/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Company/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Company/Create

        [HttpPost]
        public ActionResult Create(Company company, FormCollection collection)
        {
            try
            {
                DataStore ds = new DataStore();
                ds.Add<Company>(company);

                TempData[DataHelper.TempData.Alert] = new Alert
                {
                    Context = Alert.AlertContext.Success,
                    Markup = "Record added."
                };

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                TempData[DataHelper.TempData.Exception] = ex;
                return View();
            }
        }

        //
        // GET: /Company/Edit/5

        public ActionResult Edit(int id)
        {
            DataStore ds = new DataStore();

            return View(ds.GetList<Company>().Where(rec => rec.Id == id).FirstOrDefault());
        }

        //
        // POST: /Company/Edit/5

        [HttpPost]
        public ActionResult Edit(Company company, FormCollection collection)
        {
            try
            {
                DataStore ds = new DataStore();
                ds.Update<Company>(company);

                TempData[DataHelper.TempData.Alert] = new Alert
                {
                    Context = Alert.AlertContext.Success,
                    Markup = "Record updated."
                };

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                TempData[DataHelper.TempData.Exception] = ex;
                return View();
            }
        }

        //
        // GET: /Company/Delete/5

        public ActionResult Delete(int id)
        {
            DataStore ds = new DataStore();

            return View(ds.GetList<Company>().Where(rec => rec.Id == id).FirstOrDefault());
        }

        //
        // POST: /Company/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                DataStore ds = new DataStore();

                Company company = ds.GetList<Company>().Where(rec => rec.Id == id).FirstOrDefault();
                ds.Remove<Company>(company);

                TempData[DataHelper.TempData.Alert] = new Alert
                {
                    Context = Alert.AlertContext.Success,
                    Markup = "Record removed."
                };

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }
    }
}
